home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter2 / findtext.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  292 lines

  1.  
  2. #include <windows.h>  
  3. #include "findtext.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. HWND      hwndFind = NULL;
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "FindText()/ReplaceText()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       if ( hwndFind && IsDialogMessage( hwndFind, &msg ) )
  75.          continue;
  76.  
  77.       TranslateMessage( &msg ); 
  78.       DispatchMessage( &msg );  
  79.    }
  80.  
  81.    return( msg.wParam ); 
  82. }
  83.  
  84.  
  85. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  86. {
  87.    WNDCLASSEX wcex;
  88.  
  89.    wcex.style         = lpwc->style;
  90.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  91.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  92.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  93.    wcex.hInstance     = lpwc->hInstance;
  94.    wcex.hIcon         = lpwc->hIcon;
  95.    wcex.hCursor       = lpwc->hCursor;
  96.    wcex.hbrBackground = lpwc->hbrBackground;
  97.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  98.    wcex.lpszClassName = lpwc->lpszClassName;
  99.  
  100.    // Added elements for Windows 95.
  101.    //...............................
  102.    wcex.cbSize = sizeof(WNDCLASSEX);
  103.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  104.                             IMAGE_ICON, 16, 16,
  105.                             LR_DEFAULTCOLOR );
  106.             
  107.    return RegisterClassEx( &wcex );
  108. }
  109.  
  110. #define MAX_FIND_LEN  80
  111.  
  112. LPCTSTR lpszText = "This is a sample of text that can be searched using the"\
  113.                    " Find Text or Replace Text common dialog box.";
  114.  
  115.  
  116. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  117. {
  118. static FINDREPLACE fr;
  119. static char        szFind[MAX_FIND_LEN+1];
  120. static char        szReplace[MAX_FIND_LEN+1];
  121. static char        szBuf[512];
  122. static char        szNewBuf[512];
  123.  
  124. static UINT        uFindMessage = 0;
  125. static HWND        hEdit        = NULL;
  126. static DWORD       dwPos        = 0;
  127.  
  128.  
  129.    // Handle Find and Replace common dialog message.
  130.    //...............................................
  131.    if ( uMsg == uFindMessage )
  132.    {
  133.       LPFINDREPLACE lpfr = (LPFINDREPLACE)lParam;
  134.  
  135.       // Handle the Find Next command.
  136.       //..............................
  137.       if ( lpfr->Flags & FR_FINDNEXT )
  138.       {
  139.          LPTSTR lpFound;
  140.          DWORD  dwCurPos = SendMessage( hEdit, EM_GETSEL, 0, 0 );
  141.  
  142.          // If the position is the same as where we were,
  143.          // increment the position sow we find the next string.
  144.          //....................................................
  145.          if ( LOWORD(dwCurPos) == dwPos && dwPos > 0 )
  146.             dwPos++;
  147.          else
  148.             dwPos = LOWORD( dwCurPos );
  149.          
  150.          SendMessage( hEdit, WM_GETTEXT, sizeof( szBuf ), (LPARAM)szBuf );
  151.  
  152.          // Find the search string within the text starting at the 
  153.          // current cursor position within the edit control.
  154.          //.......................................................
  155.          lpFound = strstr( &szBuf[ LOWORD(dwPos) ], lpfr->lpstrFindWhat );
  156.  
  157.          // If the text was found, highlight it.
  158.          //.....................................
  159.          if ( lpFound )
  160.          {
  161.             dwPos = lpFound-&szBuf[0];
  162.             SendMessage( hEdit, EM_SETSEL, dwPos, 
  163.                          strlen( lpfr->lpstrFindWhat )+dwPos );
  164.          }
  165.          else
  166.             MessageBox( hWnd, "The string was not found.", 
  167.                               "Find", MB_OK | MB_ICONINFORMATION );
  168.       }
  169.  
  170.       // Handle the Replace command.
  171.       //............................
  172.       if ( lpfr->Flags & FR_REPLACE )
  173.       {
  174.          SendMessage( hEdit, WM_GETTEXT, sizeof( szBuf ), (LPARAM)szBuf );
  175.  
  176.          szBuf[dwPos] = 0;
  177.          strcpy( szNewBuf, szBuf );
  178.          strcat( szNewBuf, lpfr->lpstrReplaceWith );
  179.          strcat( szNewBuf, &szBuf[strlen( lpfr->lpstrFindWhat )+dwPos] );
  180.  
  181.          SendMessage( hEdit, WM_SETTEXT, 0, (LPARAM)szNewBuf );
  182.          SendMessage( hEdit, EM_SETSEL, dwPos, 
  183.                       strlen( lpfr->lpstrReplaceWith )+dwPos );
  184.       }
  185.  
  186.       // Handle the Replace All command.
  187.       //................................
  188.       if ( lpfr->Flags & FR_REPLACEALL )
  189.          MessageBox( hWnd, "Replace All is not implemented.", 
  190.                            "Replace", MB_OK | MB_ICONINFORMATION );
  191.  
  192.       // If the dialog is terminating, set the handle to NULL.
  193.       //......................................................
  194.       if ( lpfr->Flags & FR_DIALOGTERM )
  195.          hwndFind = NULL; 
  196.    }
  197.  
  198.    switch( uMsg )
  199.    {
  200.       case WM_CREATE :
  201.               memset( &fr, 0, sizeof( FINDREPLACE ) );
  202.  
  203.               uFindMessage = RegisterWindowMessage( "commdlg_FindReplace" );
  204.  
  205.               hEdit = CreateWindow( "EDIT", "",    
  206.                                     WS_CHILD | ES_LEFT | 
  207.                                     WS_VISIBLE | ES_MULTILINE | ES_NOHIDESEL, 
  208.                                     0, 0, 
  209.                                     0, 0,  
  210.                                     hWnd,              
  211.                                     (HMENU)101,              
  212.                                     hInst,         
  213.                                     NULL               
  214.                                   );
  215.  
  216.               SendMessage( hEdit, WM_SETTEXT, 0, (LPARAM)lpszText );
  217.               break;
  218.  
  219.       case WM_SIZE :
  220.               MoveWindow( hEdit, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
  221.               break;
  222.  
  223.       case WM_COMMAND :
  224.               switch( LOWORD( wParam ) )
  225.               {
  226.                  case IDM_FIND :
  227.                         fr.lStructSize   = sizeof( FINDREPLACE ); 
  228.                         fr.hwndOwner     = hWnd;
  229.                         fr.lpstrFindWhat = szFind;
  230.                         fr.wFindWhatLen  = MAX_FIND_LEN; 
  231.                         fr.Flags         = FR_MATCHCASE | FR_HIDEUPDOWN | FR_HIDEWHOLEWORD;
  232.  
  233.                         hwndFind = FindText( &fr );
  234.                         break;
  235.  
  236.                  case IDM_REPLACE :
  237.                         fr.lStructSize      = sizeof( FINDREPLACE ); 
  238.                         fr.hwndOwner        = hWnd;
  239.                         fr.lpstrFindWhat    = szFind;
  240.                         fr.lpstrReplaceWith = szReplace;
  241.                         fr.wFindWhatLen     = MAX_FIND_LEN; 
  242.                         fr.wReplaceWithLen  = MAX_FIND_LEN;
  243.                         fr.Flags            = FR_MATCHCASE | FR_HIDEUPDOWN | FR_HIDEWHOLEWORD;
  244.  
  245.                         hwndFind = ReplaceText( &fr );
  246.                         break;
  247.  
  248.                  case IDM_ABOUT :
  249.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  250.                         break;
  251.  
  252.                  case IDM_EXIT :
  253.                         DestroyWindow( hWnd );
  254.                         break;
  255.               }
  256.               break;
  257.  
  258.       case WM_DESTROY :
  259.               PostQuitMessage(0);
  260.               break;
  261.  
  262.       default :
  263.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  264.    }
  265.  
  266.    return( 0L );
  267. }
  268.  
  269.  
  270. LRESULT CALLBACK About( HWND hDlg,           
  271.                         UINT message,        
  272.                         WPARAM wParam,       
  273.                         LPARAM lParam)
  274. {
  275.    switch (message) 
  276.    {
  277.        case WM_INITDIALOG: 
  278.                return (TRUE);
  279.  
  280.        case WM_COMMAND:                              
  281.                if (   LOWORD(wParam) == IDOK         
  282.                    || LOWORD(wParam) == IDCANCEL)    
  283.                {
  284.                        EndDialog(hDlg, TRUE);        
  285.                        return (TRUE);
  286.                }
  287.                break;
  288.    }
  289.  
  290.    return (FALSE); 
  291. }
  292.